<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form"  :inline="true" label-width="140px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="用户姓名">
              <el-input v-model="form.realName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="手机号码">
              <el-input v-model="form.phone" />
            </el-form-item>
          </el-col>

          <!-- <el-col :span="6">
            <el-form-item label="所属平台">
              <Platform @childfun='childfun'></Platform>
            </el-form-item>
          </el-col>-->
          <el-col :span="6">
            <!-- <el-form-item> -->
            <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
            <el-button
              v-if="hasPermission('AdminUserProvider.saveAdminUser')"
              type="primary"
              @click="toRoutes('adminManageAdd')"
            >新增</el-button>
            <!-- </el-form-item> -->
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column prop="realName" label="姓名" />
        <el-table-column prop="username" label="账号" />
        <el-table-column prop="phone" label="手机号">
          <template slot-scope="scope">
            <span>{{scope.row.phone | phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="branchId" label="分公司">
          <template slot-scope="scope">
            <span>{{scope.row.branchId | companyinfo}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="deptName" label="部门" />
        <el-table-column prop="roleNames" label="角色" />
        <!-- <el-table-column
                    prop="menus"
                    label="权限"
                >
                </el-table-column>
                <el-table-column
                    prop="dataPermission"
                    label="数据范围"
                >
        </el-table-column>-->
        <el-table-column label="账号状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              class="switchStyle"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
              @click.native="changeStatus(scope.row.id,'status',scope.row.status)"
            />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              v-if="hasPermission('AdminUserProvider.updateAdminUser')"
              type="text"
              size="small"
              @click="toRoutes('adminManageEdit',scope.row.id)"
            >编辑</el-button>
            <el-button
              v-if="hasPermission('AdminUserProvider.adminUserDetails')"
              type="text"
              size="small"
              @click="toRoutes('adminManageEdit',scope.row.id,1)"
            >详情</el-button>
            <el-button
              v-if="hasPermission('AdminUserProvider.resetAdminUserPwd')"
              type="text"
              size="small"
              @click="passwordFn(scope.row.username,scope.row.id)"
            >重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 编辑弹出窗 -->
      <el-dialog
        title="重置密码"
        :visible.sync="passDialog"
        :before-close="passHandleClose"
        width="400px"
      >
        <!-- 输入框 -->
        <el-form :inline="true" label-width="80px" label-position="right">
          <el-form-item label="账户">
            <el-input v-model="nameSelect" class="borderNone" />
          </el-form-item>
          <el-form-item label="密码为">
            <span class="spanToInput">a12345678</span>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="passHandleClose()">取 消</el-button>
          <el-button type="primary" @click="passSubmit()">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import filter from "@/assets/filter.js";
import {
  getList,
  resetAdminUserPwd,
  modifyAdminUserStatus
} from "@/api/adminManage";
import Platform from "../components/platform";
export default {
  name: "AdminManageIndex",
  components: {
    Platform
  },
  data() {
    return {
      nameSelect: "",
      id: "",
      tableLoading: true,
      date: "",
      value1: "",
      options: [
        {
          value: "0",
          label: "正常"
        },
        {
          value: "1",
          label: "停用"
        }
      ],
      form: {
        realName: "",
        phone: "",
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      },
      passDialog: false,
      passForm: {
        password: ""
      }
    };
  },
  mounted() {
    this.getList(this.listForm);
  },
  methods: {
    // 搜索的
    childfun(val) {
      console.log(val);
    },
    // 按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    // 上下架与状态修改
    changeStatus(userId, type, status) {
      const datas = {
        userId: userId
      };
      datas[type] = status;
      modifyAdminUserStatus(datas).then(res => {
        // console.log(res)
        if (res.data.code === 0) {
          this.$message({
            message: "修改成功",
            type: "success"
          });
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    // 关闭修改弹窗
    passHandleClose() {
      this.passDialog = false;
    },
    // 修改弹窗
    passwordFn(name, e) {
      this.nameSelect = name;
      this.passForm.password = "";
      this.passDialog = true;
      this.id = e;
      // this.id = e
      // let datas = {
      //     id:e
      // }
      // getProjectGuaranteeById(datas).then(res=>{
      //     // console.log(res)
      //     this.form.name = res.data.data.name
      // })
    },
    // 密码修改
    passSubmit() {
      this.passDialog = false;
      this.passForm["id"] = this.id;
      console.log(this.passForm);
      const datas = {
        isShow: "1",
        userId: this.id
      };
      resetAdminUserPwd(datas)
        .then(res => {
          console.log(res);
          if (!res.data.code) {
            this.succStatus("重置");
            this.getList(this.listForm);
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(error => {
          this.errorStatus("重置");
        });
    },
    // 查询
    searchFn() {
      this.form.pageNum = 1;
      // console.log(this.form)
      this.getList(this.form);
    },
    // 路由跳转
    toRoutes(e, id, type) {
      const route = { name: e, query: { id: id, type: type } };
      this.$router.push(route);
    },
    // 列表数据获取
    getList(e) {
      this.tableLoading = true;
      const datas = e;
      getList(datas)
        .then(res => {
          console.log(res);
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.userResponseDTOS;
            this.page_totals = res.data.data.count;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 删除
    deleteFn(e) {
      const datas = {
        id: e
      };
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteData(datas)
            .then(res => {
              console.log(res);
              if (!res.data.code) {
                this.succStatus("删除");
                this.getList(this.listForm);
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(error => {
              this.$message.error(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.listForm.pageSize = val;
      this.getList(this.listForm);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.listForm.pageNum = val;
      this.getList(this.listForm);
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
